#ifndef __CSSTRING_H__
#define __CSSTRING_H__

#include "CSVector.h"
#include <string.h>


template<class Mem,int InitNum>
class CSStringBase : public CSVectorBase<char,Mem,InitNum>
{
public:
	CSStringBase() {CSVectorBase<char,Mem,InitNum>::PushFront('\0');}
	CSStringBase(const char* str);
	CSStringBase(CSStringBase<Mem,InitNum>& str);
public:
	CSStringBase& operator=(const char* str);
	CSStringBase& operator=(CSStringBase& str);
	void Connect(CSStringBase& str);
	void Connect(const char* str);
	bool operator==(const char* str);
	bool operator==(CSStringBase& str);
	bool operator!=(const char* str);
	bool operator!=(CSStringBase& str);
	char* operator*();
	int Strlen() {return CSVectorBase<char,Mem,InitNum>::GetSize()-1;}
	char* GetBuffer() {return CSVectorBase<char,Mem,InitNum>::mData;}
};

template<class Mem,int InitNum>
CSStringBase<Mem,InitNum>::CSStringBase(const char* str)
{
	CSVectorBase<char,Mem,InitNum>::Resize((int)strlen(str)+1);
	strncpy(CSVectorBase<char,Mem,InitNum>::mData,str,CSVectorBase<char,Mem,InitNum>::mNowSize);
}

template<class Mem,int InitNum>
CSStringBase<Mem,InitNum>::CSStringBase(CSStringBase<Mem,InitNum>& str)
{
	CSVectorBase<char,Mem,InitNum>::Resize((int)str.Strlen()+1);
	strncpy(CSVectorBase<char,Mem,InitNum>::mData,str.GetBuffer(),CSVectorBase<char,Mem,InitNum>::mNowSize);
}

template<class Mem,int InitNum>
CSStringBase<Mem,InitNum>& CSStringBase<Mem,InitNum>::operator=(const char* str)
{
	CSVectorBase<char,Mem,InitNum>::Resize((int)strlen(str)+1);
	strncpy(CSVectorBase<char,Mem,InitNum>::mData,str,CSVectorBase<char,Mem,InitNum>::mNowSize);
	return *this;
}

template<class Mem,int InitNum>
CSStringBase<Mem,InitNum>& CSStringBase<Mem,InitNum>::operator=(CSStringBase<Mem,InitNum>& str)
{
	CSVectorBase<char,Mem,InitNum>::Resize((int)str.GetSize());
	strncpy(CSVectorBase<char,Mem,InitNum>::mData,str.GetBuffer(),CSVectorBase<char,Mem,InitNum>::mNowSize);
	return *this;
}

template<class Mem,int InitNum>
void CSStringBase<Mem,InitNum>::Connect(CSStringBase<Mem,InitNum>& str)
{
	int oldstrlen = Strlen();
	CSVectorBase<char,Mem,InitNum>::Resize(CSVectorBase<char,Mem,InitNum>::GetSize()+str.Strlen());
	strncpy(CSVectorBase<char,Mem,InitNum>::mData+oldstrlen,str.GetBuffer(),str.GetSize());
}

template<class Mem,int InitNum>
void CSStringBase<Mem,InitNum>::Connect(const char* str)
{
	int oldstrlen = Strlen();
	int len = (int)strlen(str);
	CSVectorBase<char,Mem,InitNum>::Resize(CSVectorBase<char,Mem,InitNum>::GetSize()+len);
	strncpy(CSVectorBase<char,Mem,InitNum>::mData+oldstrlen,str,len+1);
}

template<class Mem,int InitNum>
bool CSStringBase<Mem,InitNum>::operator==(const char* str)
{
	return (strcmp(CSVectorBase<char,Mem,InitNum>::mData,str)==0);
}

template<class Mem,int InitNum>
bool CSStringBase<Mem,InitNum>::operator==(CSStringBase<Mem,InitNum>& str)
{
	return (strcmp(CSVectorBase<char,Mem,InitNum>::mData,str.GetBuffer())==0);
}

template<class Mem,int InitNum>
bool CSStringBase<Mem,InitNum>::operator!=(const char* str)
{
	return (strcmp(CSVectorBase<char,Mem,InitNum>::mData,str)!=0);
}

template<class Mem,int InitNum>
bool CSStringBase<Mem,InitNum>::operator!=(CSStringBase<Mem,InitNum>& str)
{
	return (strcmp(CSVectorBase<char,Mem,InitNum>::mData,str.GetBuffer())!=0);
}

template<class Mem,int InitNum>
char* CSStringBase<Mem,InitNum>::operator*()
{
	return GetBuffer();
}

#endif

